<template>
  <div class="basic-layout">
    <div :class="['nav-side', isCollapse ? 'fold' : 'unfold']">
      <!-- 系统Logo -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
        <span>Manager</span>
      </div>
      <!-- 导航菜单 -->
      <el-menu
        :default-active="activeMenu"
        background-color="#001529"
        text-color="#fff"
        router
        :collapse="isCollapse"
        class="nav-menu"
      >
        <tree-menu :userMenu="userMenu"></tree-menu>
      </el-menu>
    </div>
    <div :class="['content-right', isCollapse ? 'fold' : 'unfold']">
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggle">
            <el-icon><Operation /></el-icon>
          </div>
          <div class="bread">
            <BreadCrumb />
          </div>
        </div>
        <div class="user-info">
          <el-badge
            :is-dot="noticeCount > 0 ? true : false"
            class="notice"
            @click="router.push('/audit/approve')"
          >
            <el-icon><Bell /></el-icon>
          </el-badge>
          <el-dropdown @command="handleLogout">
            <span class="user-link">
              {{ userInfo.userName }}
              <el-icon class="el-icon--right"> </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="email"
                  >邮箱：{{ userInfo.userEmail }}</el-dropdown-item
                >
                <el-dropdown-item command="logout">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="wrapper">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import TreeMenu from "./TreeMenu.vue";
import BreadCrumb from "./BreadCrumb.vue";
import { ref, getCurrentInstance, computed } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
const { proxy } = getCurrentInstance();

const store = useStore();
const router = useRouter();
const userInfo = ref(store.state.userInfo);

const noticeCount = computed(() => {
  return proxy.$store.state.noticeCount;
});
const activeMenu = ref(location.hash.slice(1));

const handleLogout = (key) => {
  if (key === "email") return;
  store.commit("saveUserInfo", "");
  userInfo.value = {};
  router.push("/login");
};

const isCollapse = ref(false);

const toggle = () => {
  isCollapse.value = !isCollapse.value;
};

const getNoticeCount = async () => {
  try {
    const count = await proxy.$api.noticeCount();
    proxy.$store.commit("saveNoticeCount", count);
  } catch (err) {
    console.error(err);
  }
};
getNoticeCount();

const userMenu = ref([]);
const getMenuList = async () => {
  try {
    const { menuList, actionList } = await proxy.$api.getPermissionList();
    userMenu.value = menuList;
    store.commit("saveUserMenu", menuList);
    store.commit("saveUserAction", actionList);
  } catch (err) {
    console.error(err);
  }
};
getMenuList();
</script>

<style lang="scss">
.basic-layout {
  position: relative;
  .nav-side {
    position: fixed;
    width: 200px;
    height: 100vh;
    background-color: #001529;
    color: #fff;
    overflow-y: hidden;
    transition: width 0.3s;

    .logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;

      img {
        width: 32px;
        height: 32px;
        margin: 0 16px;
      }
    }

    .nav-menu {
      height: calc(100vh - 50px);
      border-right: none;
    }

    &.fold {
      width: 64px;
    }

    &.unfold {
      width: 200px;
    }
  }

  .content-right {
    margin-left: 200px;

    &.fold {
      margin-left: 64px;
    }

    &.unfold {
      margin-left: 200px;
    }

    .nav-top {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #ddd;
      padding: 0 20px;

      .nav-left {
        display: flex;
        align-items: center;

        .menu-fold {
          display: flex;
          align-items: center;
          margin-right: 15px;
          font-size: 18px;
        }
      }

      .user-info {
        display: flex;
        align-items: center;
        .notice {
          display: flex;
          align-items: center;
          line-height: 30px;
          margin-right: 15px;
          cursor: pointer;
        }

        .user-link {
          cursor: pointer;
          color: #409eff;
        }
      }
    }

    .wrapper {
      background: #eef0f3;
      padding: 20px;
      height: calc(100vh - 50px);
      .main-page {
        background-color: #fff;
        height: 100%;
      }
    }
  }
}
</style>
